<?php
namespace addons\TfFutures\handler;

use Yii;
use yii\db\Exception;
use yii\db\Expression;
use yii\helpers\Console;

use \addons\TfFutures\common\models\Symbol;

class GatherRandom extends Gather
{
    public static $tmpModelList = null;
    public function run(){
        $stime = time();

        if( empty(static::$tmpModelList) ){
            static::$tmpModelList = Symbol::find()->where(['on_sale'=>'on'])->orderBy(new Expression('rand()'))->asArray()->all();
        }

        foreach(static::$tmpModelList as &$tmpModel)
        {
            $ticket = Yii::$app->services->kline->loadTicekt($tmpModel['code']);
            if(empty($ticket)){

                $ticket = [
                    'ask'           => 1000,
                    'asm'           => 5000,
                    'bid'           => 1000,
                    'bim'           => 5000,
                    'price'         => 1000,
                    'price2'        => 0,
                    'volume'        => 93243,
                    'limit_up'      => 10,
                    'limit_dn'      => 10,
                    'position'      => 932343,
                    'cleaning'      => 103839,
                    'close_past'    => 1000,
                    'position_past' => 103839,
                    'cleaning_past' => 103839,
                    'symbol'        => $tmpModel['code'],
                ];
            }

            if( !empty($ticket['tm']) ){
                if( date('d')!=date('d',$ticket['tm']) ){
                    $ticket['close_past']      = $ticket['close'];
                    $ticket['position_past']   = $ticket['position'];
                    $ticket['cleaning_past']   = $ticket['cleaning'];
                }
            }

            $ticket['ask']      = static::randomPrice($ticket['ask']);
            $ticket['asm']      = static::randomPrice($ticket['ask']);
            $ticket['ask1']     = static::randomPrice($ticket['ask']);
            $ticket['ask2']     = static::randomPrice($ticket['ask']);
            $ticket['ask3']     = static::randomPrice($ticket['ask']);
            $ticket['ask4']     = static::randomPrice($ticket['ask']);
            $ticket['ask5']     = static::randomPrice($ticket['ask']);
            $ticket['bid']      = static::randomPrice($ticket['bid']);
            $ticket['bim']      = static::randomPrice($ticket['bim']);
            $ticket['bid1']     = static::randomPrice($ticket['bid']);
            $ticket['bid2']     = static::randomPrice($ticket['bid']);
            $ticket['bid3']     = static::randomPrice($ticket['bid']);
            $ticket['bid4']     = static::randomPrice($ticket['bid']);
            $ticket['bid5']     = static::randomPrice($ticket['bid']);
            $ticket['open']     = static::randomPrice($ticket['bid']);
            $ticket['close']    = static::randomPrice($ticket['bid']);
            $ticket['high']     = static::randomPrice($ticket['bid']);
            $ticket['low']      = static::randomPrice($ticket['bid']);
            $ticket['limit_up'] = static::randomPrice($ticket['limit_up'],1);
            $ticket['limit_dn'] = static::randomPrice($ticket['limit_dn'],1);
            $ticket['position'] = static::randomPrice($ticket['position']);
            $ticket['cleaning'] = static::randomPrice($ticket['cleaning']);
            $ticket['wave']     = bcdiv(bcsub($ticket['ask'],$ticket['bid'],4),$ticket['bid'],4)*100;
            $ticket['price']    = static::randomPrice($ticket['price']);
            $ticket['volume']   = static::randomPrice($ticket['volume']);
            $ticket['position'] = static::randomPrice($ticket['position']);

            $ticket['tm']       = $stime;
            $ticket['ctm']      = $stime;
            $ticket['ctmfmt']   = date('Y-m-d H:i:s',$stime);
            $tmpModel['ticekt'] = $ticket;
        }

        $this->insert(static::$tmpModelList);
    }

    public static function randomPrice( $price=1000,$float=1 ){
        $price = static::randomFloat( bcsub($price,$float,4),bcadd($price,$float,4),4 );
        return $price<1?(rand(1,100)/100):$price;
    }

    public static function randomFloat($min = 0, $max = 1) {
        return round($min + mt_rand() / mt_getrandmax() * ($max - $min),4);
    }
}